package com.zzg.job.config;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.zzg.job.Demo;
import com.zzg.listener.MyElasticJobListener;

@Configuration
public class DemoConfig {
	@Resource
	private ZookeeperRegistryCenter registryCenter;

	@Value("${demo.demoJob.name}")
	private String demoJobName;

	@Value("${demo.demoJob.cron}")
	private String demoJobCron;

	@Value("${demo.demoJob.shardingTotalCount}")
	private int demoJobShardingTotalCount;

	@Bean
	public Demo demoJob() {
		return new Demo();
	}

	@Bean(initMethod = "init")
	public JobScheduler demoJobScheduler(final Demo demoJob) {
		MyElasticJobListener elasticJobListener = new MyElasticJobListener();
		return new SpringJobScheduler(demoJob, registryCenter, liteJobConfiguration(), elasticJobListener);
		
	}

	private LiteJobConfiguration liteJobConfiguration() {
		return LiteJobConfiguration.newBuilder(new SimpleJobConfiguration(
				JobCoreConfiguration.newBuilder(demoJobName, demoJobCron, demoJobShardingTotalCount).build(),
				Demo.class.getCanonicalName())).overwrite(true).build();
	}

}
